<template>
  <div class="content">
    <p>工资社保</p>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading"
      :page-num="pageNum"
      :page-size="pageSize"
    >
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
  import {
    getMonthlyWages
  } from '@/api/teambalance'
  import myTable from '@/components/myTable'
  import global from '@/components/global'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'

  const time = {
    props: ['colConfig'],
    filters: {
      formatDate(time) {
        if (time) {
          var date = new Date(time)
          return formatDate(date, 'yyyy-MM-dd')
        }
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        {{ scope.row[colConfig.prop] | formatDate }}
      </span>
  </el-table-column>
  `
  }

  const amount = {
    props: ['colConfig'],
    mounted() {
      console.log(this.colConfig)
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
        <el-table-column v-for="(item, index) in colConfig.children"  :label="item.label" :width="item.width">
                    <span slot-scope="scope">{{scope.row[item.prop]}}</span>
        </el-table-column>
    </el-table-column>
  `
  }

  const configs = {
    props: ['colConfig'],
    filters: {
      formatDate(time) {
        if (time) {
          var date = new Date(time)
          return formatDate(date, 'yyyyMM')
        }
      },
      formatTime(time) {
        if (time) {
          var date = new Date(time)
          return formatDate(date, 'yyyyMMdd')
        }
      },
      filterInsuredStatus(type) {
        if (type) {
          let obj = {}
          obj = global.insuredStatusList.find((item) => {
            return item.value === type // 筛选出匹配数据
          })
          return obj.label
        }
      },
      filterInsuredUnit(type) {
        if (type) {
          let obj = {}
          obj = global.insuredUnitList.find((item) => {
            return item.value === type // 筛选出匹配数据
          })
          return obj.label
        }
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
            <el-table-column  v-for="(item, index) in colConfig.children"  :label="item.label" :width="item.width">
                <span slot-scope="scope" >
                    <span v-if="item.component==='month'"> {{ scope.row[item.prop] | formatDate }}</span>
                    <span v-else-if="item.component==='time'"> {{ scope.row[item.prop] | formatTime }}</span>
                    <span v-else-if="item.component==='insuredStatus'"> {{ scope.row[item.prop] | filterInsuredStatus }}</span>
                    <span v-else-if="item.component==='insuredUnit'"> {{ scope.row[item.prop] | filterInsuredUnit }}</span>
                    <span v-else >{{scope.row[item.prop]}}</span>
                </span>
            </el-table-column>
  </el-table-column>
  `
  }

  const formatter = function(row, column) {
    if (row[column.property]) {
      let obj = {}
      obj = global[`${column.property}List`].find((item) => {
        return item.value === row[column.property] // 筛选出匹配数据
      })
      return obj.label
    }
  }

  const total = function(row, column) {
    return row.netPayroll + row.total
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    props: ['viewDetailList'],
    data() {
      return {
        colConfigs: [
          { prop: 'teamName', label: '部门名称', width: '150', fixed: true },
          { prop: 'insuredName', label: '员工姓名', width: '100', fixed: true },
          { prop: 'idNumber', label: '身份证', width: '150' },
          { prop: 'position', label: '职务', width: '150' },
          { prop: 'rank', label: '职级', width: '150', formatter: formatter },
          { prop: 'pay', label: '薪资', width: '100' },
          {
            label: '基础工资', width: '200', children: [
              { prop: 'basicMoney', label: '金额', width: '100' },
              { prop: 'basicProportion', label: '占比', width: '100' }
            ], component: amount
          },
          {
            label: '绩效工资', width: '500', children: [
              { prop: 'performanceMoney', label: '金额', width: '100' },
              { prop: 'performanceProportion', label: '占比', width: '100' },
              { prop: 'score', label: '得分', width: '100' },
              { prop: 'coefficient', label: '系数', width: '100' },
              { prop: 'realization', label: '实得', width: '100' }
            ], component: amount
          },
          { prop: 'quarterlyBonus', label: '季度奖金', width: '100' },
          {
            label: '各项补贴', width: '500', children: [
              { prop: 'trafficSubsidy', label: '交通/餐饮补贴', width: '150' },
              { prop: 'otherSubsidy', label: '其他', width: '100' }
            ], component: amount
          },
          {
            label: '其他扣款', width: '500', children: [
              { prop: 'late', label: '迟到', width: '100' },
              { prop: 'absence', label: '缺勤', width: '100' },
              { prop: 'workHoliday', label: '事假', width: '100' },
              { prop: 'sickLeave', label: '病假', width: '100' },
              { prop: 'absenteeism', label: '旷工', width: '100' },
              { prop: 'subsidyCost', label: '常规补贴扣款', width: '150' },
              { prop: 'otherSubsidyCost', label: '其他', width: '100' }
            ], component: amount
          },
          {
            label: '代扣', width: '500', children: [
              { prop: 'socialSecurity', label: '社保', width: '100' },
              { prop: 'providentFund', label: '公积金', width: '100' },
              { prop: 'personTax', label: '个税', width: '100' }
            ], component: amount
          },
          { prop: 'netPayroll', label: '实发', width: '100' },
          { prop: 'moneyTime', label: '时间', width: '150', component: time },
          {
            label: '社保', width: '900', children: [
              { prop: 'socialInsuredStatus', label: '参保状态', width: '100', component: 'insuredStatus' },
              { prop: 'socialInsuredUnit', label: '参保单位', width: '150', component: 'insuredUnit' },
              { prop: 'socialStartTime', label: '参保时间', width: '150', component: 'month' },
              { prop: 'socialPayTime', label: '结算时间', width: '150', component: 'time' },
              { prop: 'socialInsuredBase', label: '缴费基数', width: '100' },
              { prop: 'socialCompanyPayment', label: '单位费用', width: '100' },
              { prop: 'socialPersonalPayment', label: '个人费用', width: '100' },
              { prop: 'unionPayme', label: '工会经费', width: '100' },
              { prop: 'openDoorFund', label: '残疾人基金', width: '100' },
              { prop: 'socialFinalMonth', label: '结算月数', width: '100' },
              { prop: 'socialSubtotal', label: '小计', width: '100' }
            ], component: configs
          },
          {
            label: '公积金', width: '900', children: [
              { prop: 'providentInsuredStatus', label: '参保状态', width: '100', component: 'insuredStatus' },
              { prop: 'providentInsuredUnit', label: '参保单位', width: '150', component: 'insuredUnit' },
              { prop: 'providentStartTime', label: '参保时间', width: '150', component: 'month' },
              { prop: 'providentPayTime', label: '结算时间', width: '150', component: 'time' },
              { prop: 'providentInsuredBase', label: '缴费基数', width: '100' },
              { prop: 'providentCompanyPayment', label: '单位费用', width: '100' },
              { prop: 'providentPersonalPayment', label: '个人费用', width: '100' },
              { prop: 'providentFinalMonth', label: '结算月数', width: '100' },
              { prop: 'providentSubtotal', label: '小计', width: '100' }
            ], component: configs
          },
          { prop: 'total', label: '总计', width: '100', formatter: total }
        ],
        listLoading: true,
        dialogType: 0,
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        pca: pca,
        rules: {},
        visible: false
      }
    },
    created() {
    },
    computed: {
      global() {
        return global
      },
      teamId() {
        return this.$store.state.user.teamId
      },
      roleType() {
        return this.$store.state.user.roleType
      }
    },
    methods: {
      fetchData() {
        this.viewDetailList.pageNum = this.pageNum
        this.viewDetailList.pageSize = this.pageSize
        this.listLoading = true
        getMonthlyWages(this.viewDetailList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      }
    },
    watch: {
      'viewDetailList': {
        handler(newValue, oldValue) {
          this.fetchData()
        },
        immediate: true
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .content {
    padding: 20px;
  }
</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
